from rest_framework.generics import ListAPIView
from rest_framework.viewsets import ModelViewSet

from goods.models import SKU, GoodsCategory, SPU, SPUSpecification
from meiduo_admin.good.sku_serialzer import SKUSerializer, SKUCategorySerializer, SPUListSerializer, \
    SPUSpecificationSerializer
from meiduo_admin.my_paginate import MyPageNumberPagination


class SKUViewSet(ModelViewSet):
    queryset = SKU.objects.all()
    serializer_class = SKUSerializer
    pagination_class = MyPageNumberPagination

    def get_queryset(self):
        keyword = self.request.query_params.get('keyword')
        if keyword:
            return SKU.objects.filter(name__contains=keyword).all()
        else:
            return SKU.objects.all()


class SKUCategoryView(ListAPIView):
    # queryset = GoodsCategory.objects.all()
    serializer_class = SKUCategorySerializer
    def get_queryset(self):
        return GoodsCategory.objects.filter(subs=None).all()

class SPUListView(ListAPIView):
    queryset = SPU.objects.all()
    serializer_class = SPUListSerializer

class SPUSpecificationListView(ListAPIView):
    serializer_class = SPUSpecificationSerializer

    def get_queryset(self):
        # 1,获取spu_id
        spu_id = self.kwargs.get("spu_id", 1)

        # 2,返回数据
        return SPUSpecification.objects.filter(spu_id=spu_id).all()

